package com.assets.Maintain;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.assets.Adapter.MyBaseExpandableListAdapter;
import com.assets.BaseActivity;
import com.assets.Ip_set.Url;
import com.assets.R;
import com.assets.bean.Scheme_Item_bean;
import com.assets.menu.Group;
import com.assets.menu.Item;
import com.assets.util.TrustAllCerts;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.http.conn.ssl.AllowAllHostnameVerifier;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Maintain_Scheme extends BaseActivity {
    private ImageView fh;
    private TextView title_text;
    private Button find,warn_add;
    private ExpandableListView exlist_lol;
    private ArrayList<Group> gData = null;
    private ArrayList<ArrayList<Item>> iData = null;
    private ArrayList<Item> lData = null;
    private String url="http://"+ Url.IP+"/api/deviceErrorQuery";
    private Context mContext;
    private MyBaseExpandableListAdapter myAdapter = null;
    @Override
    public int intiLayout() {
        return R.layout.maintain_scheme;
    }

    @Override
    public void initView() {
        mContext = this;
    exlist_lol = (ExpandableListView) findViewById(R.id.exlist_lol);
    fh = (ImageView) findViewById(R.id.fh);
    title_text = (TextView) findViewById(R.id.title_text);
    find = (Button) findViewById(R.id.find);
    warn_add = (Button) findViewById(R.id.warn_add);
    }

    @Override
    public void initData() {
    title_text.setText("异常库");
        load();
    fh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
        //为列表设置点击事件
        exlist_lol.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(mContext, "你点击了：" + iData.get(groupPosition).get(childPosition).getiName(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });

    }
    private void load() {
        new Thread(){
            @Override
            public void run() {
                try {
                    OkHttpClient client =new OkHttpClient
                .Builder()
                .hostnameVerifier(new AllowAllHostnameVerifier())
                .sslSocketFactory(TrustAllCerts.createSSLSocketFactory(), TrustAllCerts.getX509TrustManager())
                .connectTimeout(10, TimeUnit.SECONDS)//连接超时
                .readTimeout(500, TimeUnit.SECONDS)
                .build();
                    Request request = new Request.Builder()
                            .url(url)
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.i("2222222",responseData);
                    showResponse(responseData);

                } catch (IOException e) {
                    e.printStackTrace();

                }


            }
        }.start();
    }

    private void showResponse(final String responseData) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //数据准备
                gData = new ArrayList<Group>();
                iData = new ArrayList<ArrayList<Item>>();
                if (responseData.equals("[]")){
                    Toast.makeText(mContext,"无方案",Toast.LENGTH_SHORT).show();
                    return;
                }
                Gson gson = new Gson();
                List<Scheme_Item_bean> userList = gson.fromJson(responseData, new TypeToken<List<Scheme_Item_bean>>(){}.getType());
                for(int i = 0;i < userList.size();i++){
                    gData.add(new Group(userList.get(i).getErrorInfo()));
                    Log.i("123123",userList.size()+"----"+userList.get(i).getDetails().size());
                    lData = new ArrayList<Item>();
                    for (int j =0;j<userList.get(i).getDetails().size();j++){

                        lData.add(new Item(userList.get(i).getDetails().get(j).getText()));

                      Log.i("123123",iData.toString());
                    }
                    iData.add(lData);

                }
                myAdapter = new MyBaseExpandableListAdapter(gData,iData,mContext);
                exlist_lol.setAdapter(myAdapter);


            }
        });
    }
}
